<template>
  <!-- 导入表 -->

  <el-dialog title="新增字段翻译" :visible.sync="visible" width=600px top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="表名称" prop="tableName">
        <el-select v-model="queryParams.tableName" filterable placeholder="请选择表名">
          <el-option
            v-for="item in dict.type.sys_db_name"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-descriptions :column="2" style="margin-bottom: 10px" title="表信息" border>
      <el-descriptions-item label="表名称">
        {{ dbTableInfo.tableName }}
      </el-descriptions-item>
      <el-descriptions-item label="表描述">
        {{ dbTableInfo.tableComment }}
      </el-descriptions-item>
      <el-descriptions-item label="创建时间">
        <span>{{ dbTableInfo.createTime }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="更新时间">
        <span>{{ dbTableInfo.updateTime }}</span>
      </el-descriptions-item>
    </el-descriptions>

    <el-row>
      <el-table ref="table" border :data="dbTableList" @selection-change="handleSelectionChange"
                height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="columnName" label="字段名" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="columnComment" label="中文名" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-input v-model="scope.row.columnComment" size="mini" placeholder="请输入中文名"/>
          </template>
        </el-table-column>
        <el-table-column prop="isTranslated" label="翻译">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isTranslated"
              active-value="1"
              inactive-value="0"
              active-text="启用"
              inactive-text="停用"
              @change="handleTranslationChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleImportTable">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getTableInfo, importTable} from "@/api/basic/translation";

export default {
  name: "fieldTable",
  dicts: ['sys_db_name'],
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      tables: [],
      // 总条数
      total: 0,
      // 表数据
      dbTableList: [],
      // 表信息
      dbTableInfo: {},

      // 查询参数
      queryParams: {
        tableName: undefined,
        tableComment: undefined
      }
    };
  },
  methods: {
    // 显示弹框
    show() {
      // this.getList();
      this.resetForm("queryForm");
      this.dbTableInfo = {};
      this.dbTableList = [];
      this.visible = true;
    },
    // 获取表数据列表
    getList() {
      getTableInfo(this.queryParams.tableName).then(res => {
        if (res.code === 200) {
          this.dbTableInfo = res.data.dbTableInfo;
          this.dbTableList = res.data.dbTableColumns;
        }
      });
    },
    // 点击表格行切换行的选择状态
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.tables = selection.map(item => ({
        columnName: item.columnName,
        columnComment: item.columnComment,
        isTranslated: item.isTranslated
      }));
    },

    /** 搜索按钮操作 */
    handleQuery() {
      getTableInfo(this.queryParams.tableName).then(res => {
        if (res.code === 200) {
          this.dbTableInfo = res.data.dbTableInfo;
          this.dbTableList = res.data.dbTableColumns;
        }
      });
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dbTableInfo = {};
      this.dbTableList = [];
      this.handleQuery();
    },

    /** 导入按钮操作 */
    handleImportTable() {
      if (this.tables.length === 0) {
        this.$modal.msgError("请选择要导入的字段!");
        return;
      }
      // 确保提交的数据包含最新的修改
      const updatedTables = this.dbTableList
        .filter(row => this.tables.some(selected => selected.columnName === row.columnName))
        .map(row => ({
          columnName: row.columnName,
          columnComment: row.columnComment,
          isTranslated: row.isTranslated
        }));
      importTable({
        fields: updatedTables,
        tableName: this.queryParams.tableName
      }).then(res => {
        this.$modal.msgSuccess(res.msg);
        if (res.code === 200) {
          this.visible = false;
          this.$emit("ok");
        }
      });
    },

    /**
     * 切换翻译开关
     * @param row
     */
    handleTranslationChange(row) {
      if (row.isTranslated === '1' && !row.columnComment) {
        this.$modal.msgError("请先填写中文名");
        row.isTranslated = '0';
      }
    }
  }
};
</script>
